package com.popcorn.rmsystem.controller;
// 
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.action.Action;
import com.popcorn.action.ActionForward;

@WebServlet(name = "RMSystemController",urlPatterns = "*.bo")

public class RMSystemController extends javax.servlet.http.HttpServlet
      implements javax.servlet.Servlet {
   /**
    * 
    */
   private static final long serialVersionUID = 1L;

   protected void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html; charset=UTF-8");
      response.setCharacterEncoding("UTF-8");
      request.setCharacterEncoding("UTF-8");
      
      String RequestURI = request.getRequestURI(); 
      String contextPath = request.getContextPath();   
      String command = RequestURI.substring(contextPath.length());
      
      System.out.println("========================RMSystemController======================== ");
      System.out.println("RequestURI : "+ RequestURI);
      System.out.println("contextPath : "+ contextPath);
      System.out.println("command : "+ command);
      
      ActionForward forward = null;
      Action action = null;

      if (command.equals("/rms/RMSInsertDB.bo")) { // 등록  	 
         System.out.println("/rms/RMSInsertDB.bo");
         forward = new ActionForward();
         action = new ReservationAddAction();
         forward.setRedirect(false);
         forward.setPath("/rms/RMSystem.jsp");
      }      
      else if (command.equals("/rms/RMSListDB.bo")) { // 리스트
          System.out.println("/rms/RMSListDB.bo");
          forward = new ActionForward();
          action = new ReservationListAction();
          forward.setRedirect(false);
          forward.setPath("/rms/view.jsp");
      }
      else if (command.equals("/rms/RMSUpdateViewDB.bo")) { // 수정내용
          System.out.println("/rms/RMSUpdateViewDB.bo");
          forward = new ActionForward();
          action = new ReservationUpdateViewAction();
          forward.setRedirect(false);
          forward.setPath("/rms/RMSUpdate.jsp");
       }
       else if (command.equals("/rms/RMSUpdateDB.bo")) { // 수정
          System.out.println("/rms/RMSUpdateDB.bo");
          forward = new ActionForward();
          action = new ReservationUpdateAction();
          forward.setRedirect(false);
          forward.setPath("/rms/RMSView.jsp");
       }
       else if (command.equals("/rms/RMSDetailDB.bo")) { // 상세보기
          System.out.println("/rms/RMSDetailDB.bo");
          forward = new ActionForward();
          action = new ReservationDetailAction();
          forward.setRedirect(false);
          forward.setPath("/rms/RMSDetail.jsp");
       }
       else if (command.equals("/rms/RMSDeleteDB.bo")) { // 삭제
           System.out.println("/rms/RMSDeleteDB.bo");
           forward = new ActionForward();
           action = new ReservationDeleteAction();
           forward.setRedirect(false);
           forward.setPath("/rms/view.jsp");
       }
       else if (command.equals("/rms/RMSSelectDB.bo")) { // 검색
           System.out.println("/rms/RMSSelectDB.bo");
           forward = new ActionForward();
           action = new ReservationSelectAction();
           forward.setRedirect(false);
           forward.setPath("/rms/view.jsp");
       }
       else if(command.equals("/rms/RMSPCheckDB.bo")){ // 비밀번호 체크
    	   System.out.println("/rms/RMSPCheckDB.bo");
    	   forward = new ActionForward();
    	   action = new ReservationPCheckAction();
    	   forward.setRedirect(false);
    	   forward.setPath("/rms/view.jsp");
       }
    	   
		try{
		   forward = action.execute(request, response);
		}catch(Exception e){
		   e.printStackTrace();
		}
       
      if(forward != null){
		   if(forward.isRedirect()){
			   response.sendRedirect(forward.getPath());
		   }else{
			   RequestDispatcher dispatcher=
				   request.getRequestDispatcher(forward.getPath());
			   dispatcher.forward(request, response);
		   }
      }
   }

   protected void doGet(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      doProcess(request, response);
   }

   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      doProcess(request, response);
   }
}